import {baseUrl} from './config.js'

const onSiteApi = {
	login: '/api/user/login'
}

export function login(data) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + onSiteApi.login,
			method: 'POST',
			data:data,
			success(res) {
				resolve(res)
			},
			fail(e) {
				reject(e)
			}
		})
	})
}

export function req(url,params){
	return new Promise((resolve,reject)=>{
		let token=uni.getStorageSync('token');
		console.log(token)
		uni.request({
			url:baseUrl+url,
			header:{
				token
			},
			...params,
			success:res=>{
				resolve(res)
			}
		})
	})
}
/**
 * 验证token是否过期
 */
export async function verifyToken(){
	return new Promise((resolve,reject)=>{
		let token=uni.getStorageSync('token');
		if(!token){
			resolve(false);
		}
		uni.request({
			url:baseUrl+'/api/token/check',
			method:'GET',
			data:{
				token
			},
			success:res=>{
				if(res.data.code===1){
					refreshToken()
				}else{
					resolve(false);
				}
				resolve(res)
			}
		})
	})
}
/**
 * 刷新token
 */
export async function refreshToken(){
	let token=uni.getStorageSync('token');
	uni.request({
		url:baseUrl+'/api/token/refresh',
		method:'GET',
		data:{
			token
		},
		success:res=>{
			if(res.data.code===1){
				uni.setStorageSync("token",res.data.data.token);
			}
		}
	})
}
/**
 * 获取鉴权url
 */
export function getAuthentication(){
	return new Promise((resolve,reject)=>{
		uni.request({
			url:baseUrl+'/api/index/index',
			method:'GET',
			success:res=>{
				resolve(res)
			}
		})
	})
}

// 获取所有工厂

export function getAllFactory(){
	let token=uni.getStorageSync('token')
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + '/api/factory/index',
			method: 'GET',
			header:{
				token
			},
			success: res=> {
				resolve(res)
			},
			fail() {
				reject()
			}
		})
	})
}

// 获取员工信息
export function getStaffs(params){
	let token=uni.getStorageSync('token')
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + '/api/people/index',
			method: 'GET',
			data:params,
			header:{
				token
			},
			success: res=> {
				resolve(res)
			},
			fail() {
				reject()
			}
		})
	})
}

// 员工入职
export function signStaff(data) {
	let token=uni.getStorageSync('token')
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + '/api/people/sign',
			method: 'POST',
			data:data,
			header:{
				token
			},
			success: res=> {
				resolve(res)
			},
			fail() {
				reject()
			}
		})
	})
}

// 员工离职
// user_id	int	是	员工id
// factory_id	int	是	
export function leaveStaff(data) {
	let token=uni.getStorageSync('token')
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + '/api/people/leave',
			method: 'POST',
			data:data,
			header:{
				token
			},
			success: res=> {
				resolve(res)
			},
			fail() {
				reject()
			}
		})
	})
}

// 员工工时
// user_id	int	是	员工id
export function staffHour(params) {
	let token=uni.getStorageSync('token')
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + '/api/people/hour',
			method: 'GET',
			data:params,
			header:{
				token
			},
			success: res=> {
				resolve(res)
			},
			fail() {
				reject()
			}
		})
	})
}

export function claimStaff(data) {
	let token=uni.getStorageSync('token')
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + '/api/people/bind',
			method: 'POST',
			data:data,
			header:{
				token
			},
			success: res=> {
				resolve(res)
			},
			fail() {
				reject()
			}
		})
	})
}

export function checkToken() {
	let token = uni.getStorageSync('token');
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + '/api/token/check',
			method: 'GET',
			header:{
				token
			},
			success: res=> {
				resolve(res)
			},
			fail() {
				reject()
			}
		})
	})
}